#include "mainwindow.h"

#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QIcon>
#include <QDebug>
#include <QCoreApplication>
#include <QMessageBox>
#include <QStatusBar>
#include <QFileDialog>
#include <QColorDialog>
#include <QInputDialog>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

    QString dirPath = QCoreApplication::applicationDirPath();
    qDebug() << dirPath ;

    openAction = new QAction(tr("&Open"), this);
    openAction->setShortcut(QKeySequence::Open);
    openAction->setStatusTip(tr("Open a file."));
    openAction->setIcon(QIcon(":/images/folder.png"));

    colorAction = new QAction(tr("&Color"), this);
    colorAction->setShortcut(QKeySequence::Copy);
    colorAction->setStatusTip(tr("Color"));
    colorAction->setIcon(QIcon(":/images/color_swatch.png"));

    inputAction = new QAction(tr("&Input"), this);
    inputAction->setStatusTip(tr("Input"));
    inputAction->setIcon(QIcon(":/images/comment_edit.png"));

    connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
    connect(colorAction, SIGNAL(triggered()), this, SLOT(colorOpen()));
    connect(inputAction, SIGNAL(triggered()), this, SLOT(inputOpen()));

    QMenu *file = menuBar()->addMenu(tr("&File"));
    file->addAction(openAction);
    file->addAction(colorAction);
    file->addAction(inputAction);

    QToolBar *toolBar = addToolBar(tr("&File"));
    toolBar->addAction(openAction);
    toolBar->addAction(colorAction);
    toolBar->addAction(inputAction);

    msgLabel = new QLabel;
    msgLabel->setMinimumSize(msgLabel->sizeHint());
    msgLabel->setAlignment(Qt::AlignHCenter);
    statusBar()->addWidget(msgLabel);
    statusBar()->setStyleSheet(QString("QStatusBar::item{border: 0px}  "));

}

MainWindow::~MainWindow()
{

}

void MainWindow::inputOpen()
{
    bool isOK;
    QString text = QInputDialog::getText(NULL,
                                         "Input Dialog",
                                         "Please input your comment",
                                         QLineEdit::Normal,
                                         "your comment",
                                         &isOK);
    if(isOK) {
        QMessageBox::information(NULL,"Information",
                                 "Your comment is: <b>" + text + "</b>",
                                 QMessageBox::Yes | QMessageBox::No,
                                 QMessageBox::Yes);

    }

}

void MainWindow::colorOpen()
{
    QColor color = QColorDialog::getColor(Qt::white, this);
    QString msg = QString("r:%1,g:%2,b:%3").arg(QString::number(color.red()),
                                                QString::number(color.green()),
                                                QString::number(color.blue()));
    QMessageBox::information(NULL,"Selected color",msg);
}

void MainWindow::open()
{
    //QMessageBox::information(NULL, tr("open"), tr("Open a file"));
    QString path = QFileDialog::getOpenFileName(this,
                                                tr("Open Image"),
                                                ".",
                                                tr("Image Files(*.jpg *.png)"));
    if(path.length() == 0) {
        QMessageBox::information(NULL, tr("Path"), tr("You didn't select any files."));
    } else {
        QMessageBox::information(NULL, tr("Path"), tr("You selected ") + path);
    }

}
